package cn.lingnan.controller.user;


import cn.lingnan.pojo.dto.*;
import cn.lingnan.pojo.entity.PageBean;
import cn.lingnan.pojo.vo.*;
import cn.lingnan.service.UserService;
import cn.lingnan.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/pageQueryAll")
    public Result<PageBean> getAll(UserPageQueryAllDTO userPageQueryAllDTO){
        log.info("查询所有房源信息,userPageQueryAllDTO:{}",userPageQueryAllDTO);
        PageBean pageResult=userService.getAll(userPageQueryAllDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/pageQueryNewProperty")
    public Result<PageBean>  getNewProperty(UserPageSecendHandDTO userPageSecendHandDTO){
        String key="house"+userPageSecendHandDTO.getType()+"_"+userPageSecendHandDTO.getPage();
        PageBean pageResult=(PageBean)   redisTemplate.opsForValue().get(key);
        if (pageResult!=null){
            log.info("从redis中获取最新房源信息");
            return Result.success(pageResult);
        }
        log.info("查询最新房源信息,userPageSecendHandDTO:{}",userPageSecendHandDTO);
         pageResult=userService.getSecondhand(userPageSecendHandDTO);
         redisTemplate.opsForValue().set(key,pageResult);
        return Result.success(pageResult);
    }

    @GetMapping("/pageQuerySecondhand")
    public Result<PageBean> getSecondhand(UserPageSecendHandDTO userPageSecendHandDTO){
        String key="house"+userPageSecendHandDTO.getType()+"_"+userPageSecendHandDTO.getPage();
        PageBean pageResult=(PageBean)   redisTemplate.opsForValue().get(key);
        if (pageResult!=null){
            log.info("从redis中获取二手房源信息");
            return Result.success(pageResult);
        }

        log.info("查询最新二手信息,userPageSecendHandDTO:{}",userPageSecendHandDTO);
        pageResult=userService.getSecondhand(userPageSecendHandDTO);
        redisTemplate.opsForValue().set(key,pageResult);
        return Result.success(pageResult);
    }

    @GetMapping("/getPropertyById")
    public Result<PropertyVO> getProperty(Integer PropertyId){
        log.info("根据id查询房源信息,PropertyId:{}",PropertyId);
        PropertyVO propertyVO=userService.getPropertyById(PropertyId);
        return Result.success(propertyVO);
    }

    @GetMapping("/pageQueryLike")
    public Result<PageBean> getLikeProperty(UserPageLikeDTO userPageLikeDTO){
        log.info("模糊查询房源信息,userPageLikeDTO:{}",userPageLikeDTO);
        PageBean  pageResult=userService.getLikeProperty(userPageLikeDTO);
        return Result.success(pageResult);
    }

    @PutMapping("/saveFavorite")
    public Result saveFavorite(Long userId,Long propertyId){
        log.info("保存收藏信息,userId:{},propertyId:{}",userId,propertyId);
       Integer count= userService.saveFavorite(userId,propertyId);
       log.info("收藏数量:{}",count);
        return Result.success(count);

    }

    @PutMapping("/saveTransaction")
    public Result saveTransaction(Long userId,Long propertyId,double amount){
        log.info("保存交易信息,userId:{},propertyId:{},amount:{}",userId,propertyId,amount);
       Integer count= userService.saveTransaction(userId,propertyId,amount);
       log.info("交易数量:{}",count);
        return Result.success(count);
    }

    @GetMapping("/getTransactionByUserId")
    public Result getTransactionByUserId(Long userId,Integer type){
        log.info("根据用户id查询交易信息,userId:{},type:{}",userId,type);
       List<TransactionVO> transactionVO=userService.getTransactionByUserId(userId,type);
        return Result.success(transactionVO);
    }

    @DeleteMapping("/deleteTransactionById")
    public Result deleteTransactionById( Long TransactionId){
        log.info("根据交易id删除交易信息,TransactionId:{}",TransactionId);
       Integer count= userService.deleteTransactionById(TransactionId);
        return Result.success(count);
    }

    @GetMapping("/getUserById")
    public Result<UserVO> getUserById(Long userId){
        log.info("根据用户id查询用户信息,userId:{}",userId);
        UserVO userVO=userService.getUserById(userId);
        return Result.success(userVO);
    }

    @PostMapping("/updateUser")
    public Result UpdateUser(@RequestBody UserDTO userDTO){
        log.info("更新用户信息,userDTO:{}",userDTO);
        userService.updateUser(userDTO);
        return Result.success();
    }


    @GetMapping("/getReviewByPropertyId")
     public Result getReviewByPropertyId(Integer propertyId){
         log.info("根据房源id查询评论信息,propertyId:{}",propertyId);
        List<ReviewVO > reviewVO=userService.getReviewByPropertyId(propertyId);
         return Result.success(reviewVO);
     }

     @PostMapping("/saveReview")
     public Result saveReview(@RequestBody ReviewDTO reviewDTO){
        log.info("保存评论信息,reviewDTO:{}",reviewDTO);
        if(reviewDTO.getComment().length()<=0){
            return Result.error("评论内容不能为空");
        }
        userService.saveReview(reviewDTO);
        return Result.success();
     }

     @GetMapping("/getFavoriteByUserId")
    public Result getFavoriteByUserId(Long userId){
        log.info("根据用户id查询收藏信息,userId:{}",userId);
        List<FavoriteVO> favoriteVO=userService.getFavoriteByUserId(userId);
        return Result.success(favoriteVO);
    }

    @DeleteMapping("/deleteFavoriteById")
    public Result deleteFavoriteById(Long favoriteId){
        log.info("根据收藏id删除收藏信息,favoriteId:{}",favoriteId);
        userService.deleteFavoriteById(favoriteId);
        return Result.success();

    }

    @PostMapping("/saveMyPostProperty")
    public Result saveMyPostProperty(@RequestBody PropertyDTO propertyDTO){

        log.info("保存我的发布信息,propertyDTO:{}",propertyDTO);
        String key="house"+propertyDTO.getType()+"_"+"*";
        log.info("删除redis缓存:{}",key);
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
        userService.saveMyPostProperty(propertyDTO);
        return Result.success();

    }


    @GetMapping("/getMyPostProperty")
    public Result getMyPostProperty(Long userId){
        log.info("根据用户id查询我的发布信息,userId:{}",userId);
        List<MyPropertyVO> myPostVO=userService.getMyPostProperty(userId);
        return Result.success(myPostVO);
    }


    @PutMapping("/updateMyPostProperty")
    public Result StartOrStop(Long propertyId,Integer status){
        log.info("根据房源id修改房源状态,propertyId:{},status:{}",propertyId,status);
        userService.StartOrStop(propertyId,status);
        return Result.success();
    }


    @GetMapping("/getBookingsByUserId")
    public Result getBookingsByUserId(Long userId){
        log.info("根据用户id查询预订信息,userId:{}",userId);
        List<BookingVO> bookingVO=userService.getBookingsByUserId(userId);
        return Result.success(bookingVO);
    }

    @PostMapping("/saveBooking")
    public Result saveBooking( @RequestBody BookingVO bookingVO){
         log.info("保存预订信息,bookingVO:{}",bookingVO);
         userService.saveBooking(bookingVO);
         return Result.success();
    }

    @DeleteMapping("/deleteBooking")
    public Result deleteBookingById(Long bookingId) {
        log.info("根据预订id删除预订信息,bookingId:{}", bookingId);
        userService.deleteBookingById(bookingId);
        return Result.success();
    }



}
